package edu.chinasoft.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import edu.chinasoft.domain.Dept;
import edu.chinasoft.service.DeptService;

/**
 *
 * @author mhao
 * @date 2020年2月12日:下午3:56:48
 * @version V1.0
 * @Description: 
*/
@WebServlet(name = "myUpdateDeptServlet", value = "/servlet/updatedeptservlet")
public class UpdateDeptServlet extends HttpServlet {
	private static Logger logger = LoggerFactory.getLogger(UpdateDeptServlet.class);
	private DeptService deptService;
	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info(" UpdateDeptServlet >doGet start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		//获取数据
		String deptId = request.getParameter("dept_id");
		String deptName = request.getParameter("dept_name");
		String deptLocation = request.getParameter("dept_location");
		//封装到javabean
		Dept dept = new Dept();
		dept.setDeptName(deptName);
		dept.setDeptLocation(deptLocation);
		//初始化servelt里关于服务层方法成员变量
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		DeptService deptServiceImp = wac.getBean("deptService", DeptService.class);
		//调用服务层方法
		this.deptService=deptServiceImp;
		Boolean result = deptService.updateDeptById(dept);
		if(result) {
		response.sendRedirect(""+request.getContextPath()+"/servlet/showdeptservlet");
		}else {
			response.sendRedirect(""+request.getContextPath()+"/updatedeptError.jsp");
		}
		logger.info(" UpdateDeptServlet >doGet end.......");
	}

	/**
	 *
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info(" UpdateDeptServlet >doPost start.......");

		logger.info(" UpdateDeptServlet >doPost end.......");
	}

}